package com.yinshan.printer;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.util.Log;

import com.newland.me.ConnUtils;
import com.newland.mtype.ConnectionCloseEvent;
import com.newland.mtype.ModuleType;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.security.K21SecurityModule;
import com.newland.mtypex.nseries3.NS3ConnParams;
import com.nexgo.oaf.apiv3.APIProxy;
import com.szzt.sdk.device.Device;
import com.szzt.sdk.device.DeviceManager;
import com.szzt.sdk.system.SystemManager;
import com.yinshan.printer.bean.PrintData;
import com.yinshan.printer.constant.Comm;
import com.yinshan.printer.service.KSPrintService;
import com.yinshan.printer.service.N5PrintService;
import com.yinshan.printer.service.N9PrintService;
import com.yinshan.printer.utils.Logs;

import java.util.ArrayList;

public class PrinterManager {
    private final static String TAG = "PrinterManager";

    private static String MODEL = Build.MODEL;
    private static PrinterManager printerManager = null;
    private Context context;
    private boolean isDebug;

    private PrinterManager(Context context) {
        this.context = context;
    }

    public static PrinterManager createInstance(Context context) {
        init(context);
        if (null != printerManager) {
            return printerManager;
        } else {
            printerManager = new PrinterManager(context);
            return printerManager;
        }
    }

    /**
     * 必须提前初始化 否则无法首次获取tusn异常
     *
     * @param context 上下文对象
     */
    private static void init(Context context) {
        if (MODEL.equals(Comm.MODEL_KS8223)) {
            DeviceManager deviceManager = DeviceManager.createInstance(context);
            deviceManager.getSystemManager();
        }
    }

    /**
     * 打印数据
     *
     * @param printData 数据类
     */
    public void startPrint(ArrayList<PrintData> printData) {
        if (null == printData) {
            return;
        }
        Logs.d(TAG, "startPrint-->Model=" + MODEL);
        Intent intent = new Intent();
        switch (MODEL) {
            case Comm.MODEL_KS8223:
                intent.setClass(context, KSPrintService.class);
                break;
            case Comm.MODEL_N5:
                intent.setClass(context, N5PrintService.class);
                break;
            case Comm.MODEL_N900:
                intent.setClass(context, N9PrintService.class);
                break;
        }
        intent.putParcelableArrayListExtra(Comm.DATA_KEY, printData);
        context.startService(intent);
    }

    /**
     * 获取序列号tusn
     *
     * @return tusn
     */
    public String getTusn() {
        String tusn = "";
        switch (MODEL) {
            case Comm.MODEL_KS8223:
                DeviceManager deviceManager = DeviceManager.getInstance();
                SystemManager sys = deviceManager.getSystemManager();
                tusn = sys.isSupportUinonPaySN() ? sys.getDeviceInfo().getHardwareSN() : "";
                if (null != sys.getDeviceInfo() && (tusn == null || tusn.isEmpty())) {
                    tusn = "000009" + "04" + sys.getDeviceInfo().getSn();
                }
                break;
            case Comm.MODEL_N900:
                try {
                    com.newland.me.DeviceManager landDeviceManager = ConnUtils.getDeviceManager();
                    landDeviceManager.init(context, "com.newland.me.K21Driver", new NS3ConnParams(), new DeviceEventListener<ConnectionCloseEvent>() {

                        @Override
                        public void onEvent(ConnectionCloseEvent event, Handler handler) {

                        }

                        @Override
                        public Handler getUIHandler() {
                            return null;
                        }
                    });
                    landDeviceManager.connect();
                    landDeviceManager.getDevice().setBundle(new NS3ConnParams());
                    K21SecurityModule k21SecurityModule = (K21SecurityModule) landDeviceManager.getDevice().getStandardModule(ModuleType.COMMON_SECURITY);
                    tusn = k21SecurityModule.getPosTusn();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Comm.MODEL_N5:
                tusn = APIProxy.getDeviceEngine().getHSMDevice().getTUSN(0, null).getTusn();
                break;
        }
        Logs.d(TAG, "TUSN=" + tusn);
        return tusn;
    }

    public void judgePager(){
        switch (MODEL){
            case Comm.MODEL_KS8223:

                break;
            case Comm.MODEL_N900:

                break;
            case Comm.MODEL_N5:

                break;
        }
    }

    public void close() {
        Logs.d(TAG, "close()");
        if (null != printerManager) {
            printerManager = null;
        }
    }

    public boolean isDebug() {
        return isDebug;
    }

    public void setDebug(boolean debug) {
        isDebug = debug;
        Logs.isDebug = debug;
    }
}
